define(function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        var _this = this;
        return {
          collapseValue: '1',
          urls: {
            index: backend.workflowService + '/api/bench/todo',
            instanceInfo: backend.workflowService + '/api/core/myinstanceInfo',
            endInfo: backend.workflowService + '/api/core/myinstanceInfoHis',
            getComments: backend.workflowService + '/api/core/getAllComments/'
          },
          height: yufp.frame.size().height - 103,
          currentOrgId: yufp.session.org.id,
          currentUserId: yufp.session.user.userCode,
          returnBackFuncId: '',
          returnBackRootId: '',
          instanceIdInfo: {
            instanceId: '',
            flowName: '',
            flowId: '',
            flowStarter: '',
            flowStarterName: '',
            startTime: '',
            systemId: '',
            orgId: '',
            flowState: '',
            bizId: '',
            bizUserName: '',
            bizUserId: '',
            bizPage: '',
            lastNodeId: '',
            lastNodeName: '',
            handleType: '',
            nodeId: '',
            nodeName: '',
            bizType: '',
            nodeSign: '',
            nodeState: '',
            opType: {}
          },
          commentInfo: {
            instanceId: '',
            nodeId: '',
            userId: '',
            commentId: '',
            commentSign: 'O-12',
            userComment: '',
            ext: ''
          },
          flowParam: [],
          commentTableData: [],
          title: 'reset',
          nodeHistory: [],
          bizTypePage: 'nwfbiztypePage',
          commentinfo: '',
          type: '',
          bizPageInfo: '',
          bizParam: []
        };
      },
      mounted: function () { // 进入初始化
        this.type = data.type;
        this.bizParam = data.bizParam;
        this.instanceInfoFn(data);
        this.returnBackFuncId = data.returnBackFuncId;
        this.returnBackRootId = data.returnBackRootId;
      },
      methods: {
        tabClick: function (tab, event) {
          var _this = this;
          var url = _this.urls.getComments;
          if (tab.name == 'second') {
            yufp.service.request({
              method: 'GET',
              url: url,
              data: { mainInstanceId: _this.instanceIdInfo.mainInstanceId },
              callback: function (code, message, response) {
                if (response.code == 0) {
                  if (response.data == null ? false : response.data.length == 0) {
                    _this.commentinfo = '暂未有历史审批信息!';
                  }
                  var nodeHistory = response.data;
                  // commentSign翻译
                  for (var i = 0, length = nodeHistory.length; i < length; i++) {
                    nodeHistory[i].commentSign = yufp.lookup.convertKey('OP_TYPE', nodeHistory[i].commentSign);
                    // 时间格式化
                    // nodeHistory[i].startTime = yufp.util.dateFormat(nodeHistory[i].startTime, '{y}-{m}-{d}');
                  }
                  _this.nodeHistory = nodeHistory;
                } else {
                  _this.$message({ message: '获取评论意见异常:' + response.message + ';', type: 'error', duration: 6000 });
                }
              }
            });
          }
        },
        instanceInfoFn: function (param) { // 初始化实例信息
          var _this = this;
          var url = '';
          var params = { instanceId: param.instanceId };
          if (param.nodeId != '') {
            params.nodeId = param.nodeId;
          }
          if (param.type == 'DONE') {
            url = _this.urls.instanceInfo;
          } else if (param.type == 'HIS') {
            url = _this.urls.endInfo;
          }
          yufp.service.request({
            method: 'GET',
            url: url,
            data: params,
            callback: function (code, message, response) {
              if (response.code == 0) {
                if (response.data != null) {
                  _this.instanceIdInfo = response.data;
                  if (response.data.param != null) { // 流程参数初始化
                    for (var key in response.data.param) {
                      var paramTT = {};
                      paramTT.key = key;
                      paramTT.value = response.data.param[key];
                      _this.flowParam.push(paramTT);
                    }
                  }
                  if (_this.instanceIdInfo.bizPage != '') {
                    _this.collapseValue = ['1', '2'];
                  }
                  var bizPage = _this.instanceIdInfo.bizPage;
                  if (bizPage.includes('?')) {
                    var str = bizPage.split('?');
                    bizPage = str[0];
                    _this.convertParam(str[1]);
                  }
                  var paramT = {
                    instanceInfo: _this.instanceIdInfo,
                    flowParam: _this.flowParam
                  };
                  _this.$nextTick(function () {
                    try {
                      yufp.router.to(bizPage, paramT, _this.bizTypePage);
                    } catch (e) {
                      _this.bizPageInfo = '业务页面:' + _this.instanceIdInfo.bizPage + '不存在!';
                      _this.$message({ message: _this.bizPageInfo, type: 'warning' });
                    }
                  });
                }
              } else {
                _this.$message({ duration: 6000, message: '获取实例信息错误:' + response.message, type: 'error' });
                _this.cancel();
              }
            }
          });
        },
        convertParam: function (params) {
          if (params.includes(',')) {
            params = params.split(',');
            for (var k = 0; k < params.length; k++) {
              var paramf = {};
              var item = params[k];
              paramf.key = item.split('=')[0];
              paramf.value = item.split('=')[1];
              var isHave = false;
              for (var i = 0; i < this.flowParam.length; i++) {
                if (this.flowParam[i].key === paramf.key) {
                  this.flowParam[i].value = paramf.value;
                  isHave = true;
                }
              }
              if (!isHave) {
                this.flowParam.push(paramf);
              }
            }
          } else {
            var paramf = {};
            paramf.key = params.split('=')[0];
            paramf.value = params.split('=')[1];
            var isHave = false;
            for (var i = 0; i < this.flowParam.length; i++) {
              if (this.flowParam[i].key === paramf.key) {
                this.flowParam[i].value = paramf.value;
                isHave = true;
              }
            }
            if (!isHave) {
              this.flowParam.push(paramf);
            }
          }
        },
        cancel: function () { // 跳转到之前的页面
          yufp.router.to(this.returnBackFuncId, null, this.returnBackRootId);
        }
      }
    });
  };
});
